package com.dycong.common.IO.nio;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * Created by dycong on 2017/3/27.
 */
public class TestFileChannel {


    public static void main(String[] args) {
        testChanel();
        testChane0();
        testChanel1();
    }
//todo 不同方法的拷贝速度居然与文件后缀名有很大关系？？？
    public static void testChanel() {
        try (RandomAccessFile file = new RandomAccessFile("F:/资料/U盘/copyf.txt", "rw");
             RandomAccessFile fileWrite = new RandomAccessFile("F:/资料/U盘/copy1.txt", "rw")) {
            java.nio.channels.FileChannel channel = file.getChannel();
            long start = System.currentTimeMillis();
            ByteBuffer buffer = ByteBuffer.allocate(1024 * 100);
            int count = channel.read(buffer);
            while (count != -1) {
                buffer.flip();
                while (buffer.hasRemaining()) {
                    fileWrite.getChannel().write(buffer);
                }
                buffer.clear();
                count = channel.read(buffer);
            }
            System.out.println(System.currentTimeMillis() - start);
        } catch (FileNotFoundException ex) {
            throw new RuntimeException(ex);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void testChane0() {
        try (RandomAccessFile file = new RandomAccessFile("F:/资料/U盘/copyf.txt", "rw");
             RandomAccessFile fileWrite = new RandomAccessFile("F:/资料/U盘/copy0.txt", "rw")) {
            java.nio.channels.FileChannel channel = file.getChannel();
            java.nio.channels.FileChannel reader = file.getChannel();
            java.nio.channels.FileChannel writer = fileWrite.getChannel();
            long start = System.currentTimeMillis();
            writer.transferFrom(reader, 0, reader.size());
            System.out.println(System.currentTimeMillis() - start);
        } catch (FileNotFoundException ex) {
            throw new RuntimeException(ex);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void testChanel1() {
        try (RandomAccessFile file = new RandomAccessFile("F:/资料/U盘/copyf.txt", "rw");
             RandomAccessFile fileWrite = new RandomAccessFile("F:/资料/U盘/copy.txt", "rw")) {
            java.nio.channels.FileChannel channel = file.getChannel();
            FileChannel channelWrite=fileWrite.getChannel();
            long start = System.currentTimeMillis();
            MappedByteBuffer buffer = channel.map(java.nio.channels.FileChannel.MapMode.READ_WRITE, 0L, channel.size());
            MappedByteBuffer bufferWrite = channelWrite.map(java.nio.channels.FileChannel.MapMode.READ_WRITE, 0L, channel.size());


            bufferWrite.put(buffer);



            System.out.println(System.currentTimeMillis() - start);
        } catch (FileNotFoundException ex) {
            throw new RuntimeException(ex);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

